//
// Ephi - simulation of magnetic fields and particles
// Copyright (C) 2007 Indrek Mandre <indrek(at)mare.ee>
// For more information please see http://www.mare.ee/indrek/ephi/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <string.h>

#include "math3d.hpp"

void vect3d::across (vect3d& out)
{
  vect3d n = *this;
  n.normalize();
  vect3d unit[3] = {vect3d(1.0, 0.0, 0.0), vect3d(0.0, 1.0, 0.0), vect3d(0.0, 0.0, 1.0)};
  int i;
  for ( i = 0; i < 3; i++ )
    {
      out = n % unit[i];
      if ( out * out > 0.3 )
          break;
    }
  assert (i != 3);
  out.normalize();
}

void vect3d::print (const char *name) const
{
  char buf[128];
  if ( name ) printf ("%s = ", name);
  printf ("%s\n", sprint (buf));
}

char * vect3d::sprint (char *buf) const
{
  sprintf (buf, "(%g, %g, %g)", prec2double(x), prec2double(y), prec2double(z));
  return buf;
}

prec_t prec_t_sub (prec_t a, prec_t b)
{
  return a - b;
}

void transf3d::print(const char *name) const
{
  if ( name ) for ( size_t i = 0; i < strlen (name)+3; i++ ) putchar (' ');
  printf ("(%f, %f, %f)\n", prec2double(a), prec2double(b), prec2double(c));
  if ( name ) printf ("%s = ", name);
  printf ("(%f, %f, %f)\n", prec2double(d), prec2double(e), prec2double(f));
  if ( name ) for ( size_t i = 0; i < strlen (name)+3; i++ ) putchar (' ');
  printf ("(%f, %f, %f)\n", prec2double(g), prec2double(h), prec2double(i));
}
